//
//  SLQuickUI.swift
//  sulit-loan
//
//  Created by 王刚锋 on 2025/4/4.
//  Copyright © 2025 coolBoy. All rights reserved.
//

import Foundation
import SnapKit

func create_button (title:String? = "",
                    titleColor:UIColor? = .black,
                    titleFont:UIFont? = SLFontConfig.medium14,
                    bgColor:UIColor? = .clear,
                    image:UIImage?,
                    supView : UIView?,
                    closure:(_ make : ConstraintMaker) ->(),
                    selector:(target:Any?,action:Selector,event:UIControl.Event)?) -> UIButton {
    
    let button:UIButton = UIButton(type: .custom);
    button.setTitle(title, for: .normal)
    button.setTitleColor(titleColor, for: .normal)
    button.titleLabel?.font = titleFont
    button.backgroundColor = bgColor
    button.setImage(image, for: .normal)
    
    if let eventConfig = selector {
        
        button.addTarget(eventConfig.target, action: eventConfig.action, for: eventConfig.event)
    }
    if let superView = supView {
        
        superView.addSubview(button)
        button.snp.makeConstraints { (make) in
            closure(make)
        }
    }
    
    return button
}

func create_label (text:String? = "",
                    textColor:UIColor? = .black,
                    textFont:UIFont? = SLFontConfig.medium14,
                    bgColor:UIColor? = .clear,
                    alignment:NSTextAlignment? = .center,
                    supView : UIView? ,
                    closure:( _ make : ConstraintMaker) ->()) -> UILabel {

    let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
    label.text = text
    label.textColor = textColor
    label.font = textFont
    label.numberOfLines = 0
    label.backgroundColor = bgColor
    label.textAlignment = alignment ?? .center
    
    if let superView = supView {
        
        superView.addSubview(label)
        label.snp.makeConstraints { (make) in
            closure(make)
        }
    }
    return label
}

func create_view (bgColor:UIColor? = .white,
                             supView : UIView? ,
                             closure:( _ make : ConstraintMaker) ->()) -> UIView {
    
    let view:UIView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
    view.backgroundColor = bgColor
    
    if let superView = supView {
        
        superView.addSubview(view)
        view.snp.makeConstraints { (make) in
            closure(make)
        }
    }
    return view
}

func create_textField (text:String? = "",
                        textColor:UIColor? = .black,
                        textFont:UIFont? = SLFontConfig.medium14,
                        place:String? = "",
                        alignment:NSTextAlignment? = .left,
                        supView : UIView? ,
                        closure:(_ make : ConstraintMaker) ->()) -> UITextField {
    
    let textField:UITextField = UITextField(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
    textField.text = text
    textField.textColor = textColor
    textField.font = textFont
    // 设置 placeholder
    textField.placeholder = place
    if let placeholderText = place{
        let attributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: rgba(hex: "#999999") // 修改 placeholder 颜⾊
        ]
        let attributedPlaceholder = NSAttributedString(string: placeholderText, attributes: attributes)
        textField.attributedPlaceholder = attributedPlaceholder
    }

    textField.textAlignment = alignment ?? .left
    if let superView = supView {
        
        superView.addSubview(textField)
        textField.snp.makeConstraints { (make) in
            closure(make)
        }
    }
    return textField
}
func create_imageView (image:UIImage?,
                       mode:UIView.ContentMode? = UIView.ContentMode.scaleAspectFill,
                       supView : UIView? ,
                       closure:(_ make : ConstraintMaker) ->()) -> UIImageView {
    
    let imgView:UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
    imgView.contentMode = mode ?? UIView.ContentMode.scaleAspectFill
    imgView.clipsToBounds = true
    if let img = image {
        
        imgView.image = img
    }
    
    if let superView = supView {
        
        superView.addSubview(imgView)
        imgView.snp.makeConstraints { (make) in
            closure(make)
        }
    }
    
    return imgView
}

func create_scrollView (bgColor:UIColor? = .clear,
                        supView : UIView? ,
                        closure:(_ make : ConstraintMaker) ->()) -> UIScrollView {
    
    let scrollView:UIScrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
    scrollView.contentInsetAdjustmentBehavior  = .never
    scrollView.alwaysBounceVertical = true
    scrollView.showsVerticalScrollIndicator = false
    scrollView.showsHorizontalScrollIndicator = false
    if let superView = supView {
        superView.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            closure(make)
        }
    }
    return scrollView
}

func create_tableView (bgColor:UIColor? = .white,
                        supView : UIView?,
                        style: UITableView.Style? = .grouped,
                        closure:(_ make : ConstraintMaker) ->()) -> UITableView {
    
    let tableView:UITableView = UITableView(frame: CGRect(x: 0, y: 0, width: 10, height: 10), style: style!)
    tableView.separatorStyle = .none
    tableView.backgroundColor = bgColor
    tableView.estimatedRowHeight = 0
    tableView.estimatedSectionHeaderHeight = 0
    tableView.estimatedSectionFooterHeight = 0
    
    tableView.contentInsetAdjustmentBehavior = .never
    if #available(iOS 15.0, *) {
        tableView.sectionHeaderTopPadding = 0
    }
    
    if let superView = supView {
        
        superView.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            closure(make)
        }
    }
    return tableView
}

func create_control(bgColor:UIColor? = .clear,
                    supView : UIView? ,
                    closure:(_ make : ConstraintMaker) ->(),
                    selector:(target:Any?,action:Selector,event:UIControl.Event)?) -> UIControl {
    
    let control:UIControl = UIControl(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
   
    if let superView = supView {
        
        superView.addSubview(control)
        control.snp.makeConstraints { (make) in
            closure(make)
        }
    }
    return control
}


func create_gradient_image(width:CGFloat =  DevConfig.width,height:CGFloat =  DevConfig.nav_height,colors:Array<CGColor>,cornerRadius:CGFloat = 0) -> UIImage {
    
    let bgView:UIView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: height));
    
    let  gl:CAGradientLayer = CAGradientLayer()
    gl.frame = CGRect(x: 0, y: 0, width: width, height: height)
    gl.colors = colors
    gl.cornerRadius = cornerRadius
    gl.startPoint = CGPoint(x: 0.0, y: 0.5)
    gl.endPoint = CGPoint(x: 1.0, y: 0.5)
//    gl.startPoint = CGPoint(x: 0.0, y: 0.0)
//    gl.endPoint = CGPoint(x: 0.0, y: 1.0)
    bgView.layer.insertSublayer(gl, at: 0)
    
    UIGraphicsBeginImageContextWithOptions(CGSize(width: width, height: height), false, UIScreen.main.scale)
    bgView.layer.render(in: UIGraphicsGetCurrentContext()!)
    let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    return image
    
}


/// 获取渐变的图片
/// - Parameters:
///   - width: 图片宽度
///   - height: 图片高度
///   - colors: 渐变色数组
///   - startPoint: 开始位置
///   - endPoint: 结束位置
///   - cornerRadius: 图片圆角
/// - Returns: 渐变图片
func create_gradient_image(width:CGFloat =  DevConfig.width,height:CGFloat =  DevConfig.nav_height,colors:Array<CGColor>,startPoint:CGPoint = CGPoint(x: 0.0, y: 0.5),endPoint:CGPoint = CGPoint(x: 1.0, y: 0.5)) -> UIImage {
    
    let bgView:UIView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: height));
    
    let  gl:CAGradientLayer = CAGradientLayer()
    gl.frame = CGRect(x: 0, y: 0, width: width, height: height)
    gl.colors = colors
    gl.startPoint = startPoint
    gl.endPoint = endPoint
    bgView.layer.insertSublayer(gl, at: 0)
    
    UIGraphicsBeginImageContextWithOptions(CGSize(width: width, height: height), false, UIScreen.main.scale)
    bgView.layer.render(in: UIGraphicsGetCurrentContext()!)
    let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    return image
}

func create_gradient_layer(width:CGFloat = DevConfig.width,height:CGFloat = DevConfig.nav_height,colors:Array<CGColor>,layerView:UIView,cornerRadius:CGFloat) {
  
    let  gl:CAGradientLayer = CAGradientLayer()
    gl.frame = CGRect(x: 0, y: 0, width: width, height: height)
    gl.colors = colors
    gl.cornerRadius = cornerRadius
    gl.startPoint = CGPoint(x: 0.0, y: 0.5)
    gl.endPoint = CGPoint(x: 1.0, y: 0.5)
    layerView.layer.insertSublayer(gl, at: 0)
}


/// 获取虚线
/// - Parameters:
///   - width: 虚线宽度
///   - height: 虚线高度
///   - fillColor: 虚线颜色
///   - strokeColor: 实线颜色
///   - cornerRadius: 圆角
/// - Returns: <#description#>
func create_DottedLine(width:CGFloat = 5,height:CGFloat = 100,fillColor:UIColor = .red,strokeColor:UIColor = .white,cornerRadius:CGFloat)->UIView {
    
  
    let dottedLine = create_view(supView: nil) { (make) in
    }
    dottedLine.frame = CGRect(x: 0, y: 0, width: width, height: height)
    let shapeLayer:CAShapeLayer = CAShapeLayer()
    shapeLayer.bounds = CGRect(x: 0, y: 0, width: width, height: height)
    shapeLayer.position = CGPoint(x: width / 2, y: height / 2)
    //虚线部分颜色
    shapeLayer.fillColor = fillColor.cgColor
    //实线部分颜色
    shapeLayer.strokeColor = strokeColor.cgColor
    shapeLayer.cornerRadius = cornerRadius
    shapeLayer.lineWidth = width
    shapeLayer.lineJoin = CAShapeLayerLineJoin.round
    shapeLayer.lineDashPhase = 0
    shapeLayer.lineDashPattern = [NSNumber(value: 10), NSNumber(value: 20)]
    

    shapeLayer.lineDashPattern = [3,4]
    
    let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: width, height: height), cornerRadius: 5)
    shapeLayer.path = path.cgPath
    dottedLine.layer.addSublayer(shapeLayer)
    
    return dottedLine
    
}
//func alertPop(page:UIViewController,title:String = GFLanguageAdapter.localization(key: "提示"),ok:String = GFLanguageAdapter.localization(key: "确定"),cancle:String = GFLanguageAdapter.localization(key: "取消"),message:String,handle:GFAlertHandle?) {
//    
//    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
//    let okAction = UIAlertAction(title: ok, style: .default) { (_) in
//        // 点击确定按钮后执行的操作
//        print("确定按钮被点击")
//        if let handle = handle {
//            handle(1)
//        }
//    }
//    alertController.addAction(okAction)
//    let cancelAction = UIAlertAction(title: cancle, style: .destructive) { (_) in
//        // 点击取消按钮后执行的操作
//        print("取消按钮被点击")
//        if let handle = handle {
//            handle(0)
//        }
//    }
//    alertController.addAction(cancelAction)
//
//    // 在UIViewController中显示警告对话框
//    page.present(alertController, animated: true, completion: nil)
//}
//
//
//
//func alertSheet(page:UIViewController,title:String = "",message:String = "",items:[String] = [], handle: @escaping(GFAlertHandle)) -> Void {
//    let actionSheet = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
//    for i in 0..<items.count {
//        let action = UIAlertAction(title: items[i], style: .default) { (action) in
//            // Handle Action 1
//            handle(i)
//        }
//        actionSheet.addAction(action)
//    }
//    let cancelAction = UIAlertAction(title: "取消", style: .cancel) { (action) in
//        // Handle Cancel
//    }
//    actionSheet.addAction(cancelAction)
//    // Present the action sheet on a view controller
//    page.present(actionSheet, animated: true, completion: nil)
//}
extension UIImageView{
    func setUrlImage(url:String = "",placeholder:UIImage = IMAGE("")) -> Void {
        if url.contains("http"){
            if let path = URL(string: url){
                self.kf.setImage(with: path,placeholder: placeholder)
            }
        }else{
            self.image = placeholder
        }
    }
}
